SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_PERIOD_STATUSES';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_PERIOD_STATUSES;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO GL_PERIOD_STATUSES VALUES
			(
		C1.APPLICATION_ID                 ,
		CAPBRIMP_MAPPING.GetSOBIDMapping(C1.SET_OF_BOOKS_ID)                ,
		C1.PERIOD_NAME                    , 
		C1.LAST_UPDATE_DATE      , 
		C1.LAST_UPDATED_BY                , 
		C1.CLOSING_STATUS                 ,
		C1.START_DATE            ,
		C1.END_DATE              ,
		C1.YEAR_START_DATE       ,
		C1.QUARTER_NUM                    ,
		C1.QUARTER_START_DATE    ,
		C1.PERIOD_TYPE                    ,
		C1.PERIOD_YEAR                    ,
		C1.EFFECTIVE_PERIOD_NUM           ,
		C1.PERIOD_NUM                     ,
		C1.ADJUSTMENT_PERIOD_FLAG         ,
		C1.CREATION_DATE         ,
		C1.CREATED_BY                     ,
		C1.LAST_UPDATE_LOGIN              ,
		C1.ELIMINATION_CONFIRMED_FLAG     ,
		C1.ATTRIBUTE1                     ,
		C1.ATTRIBUTE2                     ,
		C1.ATTRIBUTE3                     ,
		C1.ATTRIBUTE4                     ,
		C1.ATTRIBUTE5                     ,
		C1.CONTEXT                        ,
		C1.CHRONOLOGICAL_SEQ_STATUS_CODE  
			);

		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
